package com.hwadee.ems.service.impl;

import jakarta.annotation.PostConstruct;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.hwadee.ems.domain.dto.PageQuery;
import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.po.Class;
import com.hwadee.ems.domain.vo.ClassVO;
import com.hwadee.ems.mapper.ClassMapper;
import com.hwadee.ems.service.IClassService;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * <p>
 * 班级 服务实现类
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements IClassService {

    private static IClassService instance;

    public static IClassService getInstance() {
        return instance;
    }

    @PostConstruct
    void init() {
        instance = this;
    }

    @Override
    public Result<IPage<ClassVO>> page(PageQuery pageQuery, ClassVO classVO) {
        List<ClassVO> vos = baseMapper.selectPageByConditions(
            Optional.ofNullable(classVO.getGrade()).map(Object::toString).orElse("null"),
            classVO.getMajorName(),
            classVO.getName(),
            classVO.getDepartmentName(),
            pageQuery.getSize(),
            (pageQuery.getCurrent() - 1) * pageQuery.getSize()
        );
        IPage<ClassVO> page = Page.of(pageQuery.getCurrent(), pageQuery.getSize());
        page.setTotal(count());
        page.setRecords(vos);
        return Result.ok(page);
    }

}